/*
 * $Id: CompareWithHeadAction.java 67 2002-11-03 18:37:46Z bpruessmann $
 * 
 * Copyright (c) 2001,2002 Boris Pruessmann and others.
 * All rights reserved.   This program and the accompanying materials
 * are made available under the terms of the Common Public License v0.5
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/cpl-v05.html
 * 
 * Contributors:
 *    Boris Pruessmann - Initial implementation.
 */
package net.sourceforge.perforce.ui.action;

import java.lang.reflect.InvocationTargetException;

import net.sourceforge.perforce.core.PerforceCorePlugin;
import net.sourceforge.perforce.core.PerforceException;
import net.sourceforge.perforce.core.internal.P4TeamProvider;
import net.sourceforge.perforce.core.resources.RemoteResource;
import net.sourceforge.perforce.ui.PerforceCompareEditorInput;
import net.sourceforge.perforce.ui.PerforceResourceEditionNode;
import net.sourceforge.perforce.ui.PerforceResourceNode;
import org.eclipse.compare.CompareUI;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.operation.IRunnableWithProgress;
import org.eclipse.team.core.RepositoryProvider;
import org.eclipse.team.core.TeamException;

/**
  * @version $Revision: 67 $
  * @author  <a href="mailto:bpruessmann@users.sourceforge.net">Boris Pruessmann</a>
  */
public class CompareWithHeadAction extends PerforceAction
{
	// ------------------------------------------------------------------------------ Public Methods
	
	/** @see IActionDelegate#run(IAction) */
	public void run(IAction action)
	{
		run(new IRunnableWithProgress() 
		{
			public void run(IProgressMonitor monitor) 
				throws InvocationTargetException 
			{
				try 
				{
					IResource[] resources = getSelectedResources();
					if (resources.length != 1) 
					{
						return;
					}
						
					P4TeamProvider provider = 
						P4TeamProvider.getProvider(resources[0]);
					
					RemoteResource remoteResource = 
						provider.getRemoteTree(resources[0], "#head", new NullProgressMonitor());
					RemoteResource ancestorResource = 
						provider.getRemoteTree(resources[0], "#have", new NullProgressMonitor());

					CompareUI.openCompareEditor(
						new PerforceCompareEditorInput(new PerforceResourceNode(resources[0]), 
						new PerforceResourceEditionNode(remoteResource), 
						new PerforceResourceEditionNode(ancestorResource)));
				} 
				catch (TeamException e) 
				{
					throw new InvocationTargetException(e);
				}
			}
		}, PROGRESS_BUSYCURSOR);		
	}

	// --------------------------------------------------------------------------- Protected Methods

	/** @see TeamAction#isEnabled() */
	protected boolean isEnabled() 
		throws PerforceException
	{
		IResource[] resources = getSelectedResources();
		if (resources.length != 1)
		{
			return false;
		}
			
		RepositoryProvider tempProvider = RepositoryProvider.getProvider(resources[0].getProject());
		if (tempProvider == null || !tempProvider.getID().equals(PerforceCorePlugin.getTypeId()))
		{
			return false;
		}
				
		P4TeamProvider provider = (P4TeamProvider) tempProvider;
		if (!(provider.hasRemote(resources[0])))
		{
			return false;
		}
				
		return true;
	}
}
